<template>
	<view class="shoucang">
		<headertop str1="oridei" title="关键词"></headertop>
		<view class="shoucangback">

		</view>
		<view class="centerbox">
			<view class="allspbox">
				<view class="msgicon" v-if="!List.length">
					<image src="https://chunchi.oss-cn-beijing.aliyuncs.com/static/Frame@2x(21).png" mode="widthFix"></image>
					<view class="msgtext">
						暂无商品
					</view>
				</view>
				<view class="itembox" v-for="item in  List" :key="item.id" @click="gopage(item.id)">
					<image :src="item.image" class="fmimg" mode=""></image>
					<view class="texts">
						<view class="t1">
							{{item.title}}
						</view>
						<view class="t2">
							{{item.subtitle}}
						</view>
						<view class="jiageline">
							<view class="jige">
								￥{{jigaefun(item.price[0])}}.<span
									style="font-size: 20rpx;">{{jigaefun(item.price[0],'后')}}</span>
							</view>
							<image src="https://chunchi.oss-cn-beijing.aliyuncs.com/static/Frame@2x(3).png" @click.stop="add('+',item)"
								class="gwcicon" mode=""></image>
						</view>
					</view>
				</view>
			</view>
		</view>
		<userlogin ref="denglu"></userlogin>
	</view>
</template>

<script setup>
	import headertop from '@/components/header.vue'
	import {
		onLoad,
		onShow,
		onPageScroll,
		onReachBottom
	} from "@dcloudio/uni-app";
	import {
		addcart,
	} from '@/api/home.js'
	import userlogin from '@/components/getuser/getuser.vue'
	import {
		goods
	} from '@/api/home.js'
	import {
		ref,
		onMounted,
		reactive,
	} from 'vue'
	const denglu = ref(null)
	const List = ref([])
	const total = ref(0)
	const query = reactive({
		page: 1,
		limit: 10,
		keyword: '',
		category_id: ''
	})
	onLoad((opt) => {
		query.keyword = opt.keywords
		init()
	})
	onReachBottom((e) => {
		if (total.value > List.value.length) {
			query.page++
			init()
		}
	})
	async function add(type, item) {
		if (!uni.getStorageSync('token')) {
			denglu.value.DengLu()
			return
		}
		var obj = {
			goods_id: item.id,
			goods_num: 1,
			goods_sku_price_id: item.new_sku_prices[0]['id'],
			type: type == '+' ? 'inc' : 'dec'
		}
		const res = await addcart(obj)
		if (res.code == 1) {
			uni.switchTab({
				url: '/pages/gouwuche/index'
			})
		} else {
			uni.showToast({
				title: res.msg,
				icon: 'none'
			})
		}
	}
	async function init() {
		const res = await goods(query)
		if (res.code == 1) {
			List.value.push(...res.data.data)
			total.value = res.data.total
		}
	}

	function gopage(id) {
		uni.navigateTo({
			url: '/pages/home/shangpdetail?id=' + id
		})
	}

	function jigaefun(str1, strr) {
		let number = str1
		let str = number.toString(); // 转换为字符串 "100.00"
		let parts = str.split('.'); // 按小数点分割
		let integerPart = parts[0]; // "100"
		let decimalPart = parts[1] || "00"; // "00"（如果没有小数部分，默认 "00"）
		if (!strr) {
			return integerPart
		} else {
			return decimalPart
		}

	}
</script>

<style lang="scss">
	.msgicon {
		width: 608rpx;
		margin: 100rpx auto;
		overflow: hidden;

		.msgtext {
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			font-size: 28rpx;
			color: #86909C;
			text-align: center;
			width: 100%;
			margin-top: -30rpx;
		}

		image {
			width: 608rpx;
		}
	}

	.shoucang {
		width: 100%;
		position: relative;
		overflow: hidden;

		.shoucangback {
			width: 750rpx;
			height: 314rpx;
			background: linear-gradient(180deg, #FCE6CD 0%, rgba(252, 230, 205, 0) 100%);
			position: absolute;
			z-index: -1;
		}

		.centerbox {
			width: 686rpx;
			margin: auto;
			margin-top: 184rpx;
			overflow: hidden;

			.allspbox {
				margin-top: 28rpx;
				overflow: hidden;
				display: flex;
				flex-wrap: wrap;
				justify-content: space-between;
				align-items: center;
				width: 100%;

				.itembox {
					width: 334rpx;
					height: 492rpx;
					background: #F7F8FA;
					border-radius: 20rpx 20rpx 20rpx 20rpx;
					overflow: hidden;
					display: flex;
					flex-direction: column;
					align-items: center;
					margin-bottom: 24rpx;

					.fmimg {
						width: 318rpx;
						height: 318rpx;
						margin-top: 8rpx;
						border-radius: 16rpx 16rpx 0rpx 0rpx;
					}

					.texts {
						width: 318rpx;
						margin: auto;
						overflow: hidden;
						margin-top: 18rpx;

						.jiageline {
							width: 100%;
							display: flex;
							justify-content: space-between;
							align-items: center;
							margin-top: 24rpx;

							.jige {
								font-family: PingFang SC, PingFang SC;
								font-weight: bold;
								font-size: 32rpx;
								color: #F53F3F;
							}

							.gwcicon {
								width: 36rpx;
								height: 36rpx;
								margin-right: 24rpx;
							}
						}

						.t1 {
							font-family: PingFang SC, PingFang SC;
							font-weight: 500;
							font-size: 24rpx;
							color: #1D2129;
							overflow: hidden;
							text-overflow: ellipsis;
							display: -webkit-box;
							-webkit-line-clamp: 1;
							/* 限制行数为2 */
							-webkit-box-orient: vertical;
						}

						.t2 {
							font-family: PingFang SC, PingFang SC;
							font-weight: 400;
							font-size: 22rpx;
							color: #4E5969;
							overflow: hidden;
							text-overflow: ellipsis;
							display: -webkit-box;
							-webkit-line-clamp: 1;
							/* 限制行数为2 */
							-webkit-box-orient: vertical;
							margin-top: 14rpx;
						}
					}
				}
			}

			.linetop {
				width: 100%;
				display: flex;
				justify-content: space-between;
				align-items: center;

				.title {
					font-family: PingFang SC, PingFang SC;
					font-weight: bold;
					font-size: 28rpx;
					color: #4E5969;
				}

				.glsc {
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					font-size: 24rpx;
					color: #1D2129;
				}
			}
		}
	}
</style>